package com.yd.mq.parse.utils;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;

/**
 * 文件读取服务类
 * 
 * @ClassName FileReadingService
 * @author ZuoLG
 * @date 2017年8月24日 下午5:12:04
 */
@Component
public class FileReadingUtils {

	/* 文件跟目录 */
	@Value("${ibmmq.file.path}")
	private String						rootPath		= null;

	/* 日志处理 */
	private Logger						log				= Logger.getLogger(this.getClass().getName());

	/* 配置数据所在的路径 */
	@SuppressWarnings("unused")
	private static final String			DATA_ELEMENT	= "FM_ALARM_MSG";

	@SuppressWarnings("unused")
	@Autowired
	private Alarm2DBService				service;

	@Autowired
	private AlarmParseUtils				parseUtil;

	/* 并发处理 */
//	private ListeningExecutorService	executorService	= MoreExecutors
//			.listeningDecorator(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2));

	/**
	 * 读取文件路径 @return void @throws
	 */
	@Async
	public void readFilePath(Date start_time, Date end_time) {
		// 返回所有时间段的路径
		List<String> paths = getDatePathList(start_time, end_time);
		log.info("共找到文件路径数:" + paths.size());
		for (String path : paths) {
			readFile(path);
		}
	}

	/**
	 * 读取指定文件夹下的所有文件 @return void @throws
	 */
	private void readFile(String path) {
		log.info(rootPath + path);
		File rootFile = new File(rootPath + path);
		// 如果根目录不存在，跳过
		if (!rootFile.isDirectory()) {
			log.info("文件根目录不存在！");
			return;
		}

		File[] timeFiles = rootFile.listFiles();// get所有文件
		for (File fileXml : timeFiles) {
			if (fileXml.exists()) {
				log.info("解析文件：" + fileXml.getAbsolutePath());

				try {
					parseUtil.init(fileXml, fileXml);
				} catch (Exception e) {

					log.info("异常文件：" + fileXml.getAbsolutePath());
					e.printStackTrace();
					log.info("解析文件异常！" + e.getMessage());
				}

			}
		}

	}

	/**
	 * 返回文件的路径列表 @return List<String> @throws
	 */
	@SuppressWarnings("deprecation")
	private List<String> getDatePathList(Date start_time, Date end_time) {

		List<String> list = new ArrayList<>();
		list.add(datePath(start_time));
		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		// long ns = 1000;
		// 获得两个时间的毫秒时间差异
		long diff = end_time.getTime() - start_time.getTime();
		// 计算差多少天
		long day = diff / nd;
		// 计算差多少小时
		long hour = diff % nd / nh;
		int index = (int) (day * 24 + hour);

		for (int i = 0; i < index; i++) {
			start_time.setHours(start_time.getHours() + 1);
			list.add(datePath(start_time));
		}

		return list;
	}

	/**
	 * 返回指定日期的文件路径
	 */
	private String datePath(Date date) {
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd-HH");
		String path = sd.format(date);
		path = path.replace("-", File.separator);
		return path + File.separator;
	}

	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws ParseException {
		FileReadingUtils fileTest = new FileReadingUtils();
		fileTest.rootPath = "F:\\IDEA\\MQ\\";
		Date date = new Date();
		date.setHours(date.getHours() - 1);

		List<String> paths = fileTest.getDatePathList(date, date);
		for (String path : paths) {
			fileTest.readFile(path);
		}

	}

}
